/**
 * This class is for the search functionality
 * @author Lee Li Hui
 */

package TimeSqueeze.logic;
import TimeSqueeze.common.*;
import TimeSqueeze.storage.TempStorage;

import java.util.ArrayList;

public class Search implements CommandInterface {
	
	private static final String MESSAGE_TASK_NOT_FOUND = "No such tasks found.";
	
	private static TaskInterface taskInterInput;
	private static Task taskInput;
	private static DeadlineTask deadlineTaskInput;
	private static TimedTask timedTaskInput;
	private static String content; //keyword from description or tag
	private static Time time; //start or end time
	private static ArrayList<Integer> indexOfTimed = new ArrayList<Integer>();
	private static ArrayList<Integer> indexOfUntimed = new ArrayList<Integer>();
	private static ArrayList<Integer> indexOfDeadline = new ArrayList<Integer>();
	
	public Search(Task task) {
		taskInput = task;
	}
	
	public Search(DeadlineTask task) {
		deadlineTaskInput = task;
	}
	
	public Search(TimedTask task) {
		timedTaskInput = task;
	}
	
	public Search(TaskInterface inputTask) {
		taskInterInput = inputTask;
	}
	
	private static void getTaskIndexViaContent() {
		//search untimed tasks
		for (int i = 0; i < TempStorage.untimedTasks.size(); i++) {
			if (TempStorage.untimedTasks.get(i).getDescription().contains(content) ||
					TempStorage.untimedTasks.get(i).getTag().contains(content)) {
				indexOfUntimed.add(i);
			}
		}

		//search timed tasks
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getDescription().contains(content) ||
					TempStorage.timedTasks.get(i).getTag().contains(content)) {
				indexOfTimed.add(i);
			}
		}

		//search deadlined tasks
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getDescription().contains(content) ||
					TempStorage.deadlineTasks.get(i).getTag().contains(content)) {
				indexOfDeadline.add(i);
			}
		}
	}
	
	private static void getTaskIndexViaTime() {
		//search timed tasks
		for (int i = 0; i < TempStorage.timedTasks.size(); i++) {
			if (TempStorage.timedTasks.get(i).getStartTime() == time ||
					TempStorage.timedTasks.get(i).getEndTime() == time || 
					(TempStorage.timedTasks.get(i).getStartTime().isAfter(time) && !TempStorage.timedTasks.get(i).getEndTime().isAfter(time))) {
				indexOfTimed.add(i);
			}
		}

		//search deadlined tasks
		for (int i = 0; i < TempStorage.deadlineTasks.size(); i++) {
			if (TempStorage.deadlineTasks.get(i).getEndTime() == time ||
					(TempStorage.deadlineTasks.get(i).getEndTime().getDay() == time.getDay() && TempStorage.timedTasks.get(i).getStartTime().isAfter(time))) {
				indexOfDeadline.add(i);
			}
		}
	}
	
	private static void displaySearchResults(){
		//print untimed tasks
		if (!indexOfUntimed.isEmpty()) {
			System.out.println("*****Untimed Tasks*****");
			
			for (int i = 0; i < indexOfUntimed.size(); i++) {		
				System.out.println(TempStorage.untimedTasks.get(i).getDescription());
				System.out.println("Tag: "+TempStorage.untimedTasks.get(i).getTag());
				System.out.println();
			}
		}
		
		//print timed tasks
		if (!indexOfTimed.isEmpty()) {
			System.out.println("*****Timed Tasks*****");
			
			for (int i = 0; i < indexOfTimed.size(); i++){
				System.out.println(TempStorage.timedTasks.get(i).getDescription());
				System.out.println("Start Time: " + TempStorage.timedTasks.get(i).getStartTime().toString()
						+"  " + "End Time: " + TempStorage.timedTasks.get(i).getEndTime().toString());
				System.out.println("Tag: " + TempStorage.timedTasks.get(i).getTag());
				System.out.println();
			}
		}
		
		//print deadline tasks
		if (!indexOfDeadline.isEmpty()) {
			System.out.println("*****Deadline Tasks*****");
			
			for (int i = 0; i < indexOfDeadline.size(); i++){
				System.out.println(TempStorage.deadlineTasks.get(i).getDescription());
				System.out.println("Deadline: " + TempStorage.deadlineTasks.get(i).getEndTime().toString());
				System.out.println("Tag: " + TempStorage.deadlineTasks.get(i).getTag());
				System.out.println();
			}
		}
	}
	
	@Override
	public void execute() {
		if (taskInput != null) {
			if (taskInput.getDescription() != null) {
				content = taskInput.getDescription();
			} else {
				content = taskInput.getTag();
			}
		}
		
		if (deadlineTaskInput != null) {
			if (deadlineTaskInput.getDescription() != null) {
				content = deadlineTaskInput.getDescription();
			} else if (deadlineTaskInput.getTag() != null) {
				content = deadlineTaskInput.getTag();
			} else if (deadlineTaskInput.getEndTime() != null) {
				time = deadlineTaskInput.getEndTime();
			}
		}
		
		if (timedTaskInput != null) {
			if (timedTaskInput.getDescription() != null) {
				content = timedTaskInput.getDescription();
			} else if (timedTaskInput.getTag() != null) {
				content = timedTaskInput.getTag();
			} else if (timedTaskInput.getStartTime() != null) {
				time = timedTaskInput.getStartTime();
			} else {
				time = deadlineTaskInput.getEndTime();
			}
		}
		
		if (!content.equals("")) {
			getTaskIndexViaContent();
			displaySearchResults();
		} else if (time != null) {
			getTaskIndexViaTime();
			displaySearchResults();
		} else {
			System.out.println(MESSAGE_TASK_NOT_FOUND);
		}
	}
	
	@Override
	public void undo() {
		// TODO Auto-generated method stub

	}
}
